/**
 * config.columns=[]; config.columns.name 代表要取的字段名称
 * 
 */
(function($) {
	//当前容器id
	var currentContainerId;
	//当前配置（当前页，总页数，表格对象等）
	var currentContainerConfig;
	//存储多个currentContainerConfig的map,key为currentContainerId
	var containerConfigMap={};
	
	function createTableBody(data) {
		var tableNode=currentContainerConfig.tableNode;
		var columns=currentContainerConfig.columns;
		$('tr:not(:first)', tableNode).remove();
		var header = $('tr:first', tableNode);
		var list = data.list;
		window.gridcache=list;
		if (list && list.length > 0) {
			var html = "";
			for ( var i = 0; i < list.length; i++) {
				html += "<tr><td height='42' align='center'>"+(i+1)+"</td>";
				var row = list[i];
				for ( var j = 0; j < columns.length; j++) {
					if(columns[j].renderer!=null){
						//						console.log(columns[j].renderer.call(row,row[columns[j].name]));
						html +="<td align='center'  style='"+(columns[j].style==null?'':columns[j].style)+"'>" +  columns[j].renderer.call(row,row[columns[j].name]) + '</td>';
					}
					else{
						html += "<td align='center'  >" + (row[columns[j].name]==null?'':row[columns[j].name]) + '</td>';
					}
				}
			}
			html += "<tr>";
			header.parent().append(html);
		}
	}
	function createPagingBar(data) {
		$('#'+currentContainerId+' .page').remove();
		var pageBar2 = "<div id='page' class='page'>共有"+data.count+"条信息，当前<strong><input  id='goto'   value='"+data.pageNo+"'>/"+data.pageNum+" </strong>页";
		pageBar2 +="<a  href='javascript:$.page(1,"+data.pageNum+")'>首页</a>  <a href='javascript:$.page("+(data.pageNo-1)+","+data.pageNo+")'>上一页</a> <a href='javascript:$.page("+(data.pageNo+1)+","+data.pageNum+")'>下一页</a> <a href='javascript:$.page("+data.pageNum+","+data.pageNum+")'>尾页</a><a href='javascript:$.page("+data.pageNo+","+data.pageNum+")'>   刷新 </a></div>";
		currentContainerConfig.tableNode.parent().append(pageBar2);
		//		$("#goto").bind("blur",function(){
		//		$.page(this.value,data.pageNum);
		//		});
		$("#"+currentContainerId+" #goto").bind("keyup",function(event){
			if (event.keyCode=="13"){
				var re = /^[1-9]+[0-9]*]*$/;        //判断正整数    
				if (!re.test(this.value))
				{
					alert("请输入数字(例如:1)");
					this.value= 1;
					this.focus();
					return false;
				}else {
					$.page(this.value,data.pageNum);
				}
			}
		});
	}


	$.extend({page:function(pageNo,totalNum) {
		currentContainerConfig.pageNo = parseInt(pageNo);
		if(typeof(pageNo)!='number') {
			alert("请输入正确的页码");
			return;
		}else{
			if(pageNo<=1) {
				currentContainerConfig.pageNo = 1;
			}
			if(pageNo>=totalNum) {
				currentContainerConfig.pageNo = totalNum;
			}
			currentContainerConfig.params.pageNo=pageNo;
			$.post(currentContainerConfig.url, currentContainerConfig.params, function(data) {
				createTableBody(data);
				createPagingBar(data);
				currentContainerConfig.currPageNo = pageNo;
			});
		}
	}
	});

	//	鼠标滑入table
	$.extend({fmove:function(event){   
		//		var tr=event.target.parentNode;  
		//		var table = tr.parentNode;
		//		if(table.children[0] != tr) {
		//		var array = tr.children; 
		//		for ( var i = 0; i < array.length; i++) {
		//		var td = array[i];
		//		td.style.backgroundColor="#fef7f0";
		//		}
		//		}
	}
	}); 
	//	鼠标滑出table
	$.extend({fout:function(event){   
		//		var tr=event.target.parentNode;  
		//		var table = tr.parentNode;
		//		if(table.children[0] != tr) {
		//		var array = tr.children; 
		//		for ( var i = 0; i < array.length; i++) {
		//		var td = array[i];
		//		td.style.backgroundColor="white";
		//		}
		//		}
	}

	}); 
	//清空配置缓存
	$.extend({cleanPage:function(containerId){   
		if(containerConfigMap[containerId]){
			containerConfigMap[containerId]=null;
		};
	}
	}); 


	$.fn.table = function(config,ispage,isfirst) {
		var dobj=$(this);
		currentContainerId=dobj.attr("id");
		//根据id创建一套缓存配置（如果不存在）
		if(!containerConfigMap[currentContainerId]){
			currentContainerConfig={};
			currentContainerConfig.isFirst = true;
			currentContainerConfig.tableNode = null;
			currentContainerConfig.columns = null;
			currentContainerConfig.url = null;
			currentContainerConfig.defaultPageSize = 5;
			currentContainerConfig.pageSize = currentContainerConfig.defaultPageSize;
			currentContainerConfig.params = null;
			currentContainerConfig.isPage = true;
			currentContainerConfig.currPageNo = 1;
			containerConfigMap[currentContainerId]=currentContainerConfig;
		}
		currentContainer=containerConfigMap[currentContainerId];
		currentContainerConfig.isFirst=isfirst;
		if(currentContainerConfig.isFirst) {
			var html = "<table class='jquery_table' width='900' border='0'  cellspacing='0' cellpadding='0' onmousemove='$.fmove(event)'   onmouseout='$.fout(event)'><tr>";
			html += "<th class='bk' width='54' height='26' scope='col'>序号 </th>";
			currentContainerConfig.columns = config.columns;
			for ( var i = 0; i < currentContainer.columns.length; i++) {
				html += "<th align='center'  height='26'>" + currentContainer.columns[i].headerText + '</th>';
			}
			html += "</tr>";	
			html += '</table>';
			currentContainerConfig.tableNode = $(html);

			currentContainerConfig.isFirst = false;
		}

		$(this).append(currentContainerConfig.tableNode);
		currentContainerConfig.url = config.url;
		currentContainerConfig.pageSize = config.pageSize;
		currentContainerConfig.params = config.params;
		currentContainerConfig.params.pageNo=currentContainerConfig.params.pageNo&&currentContainerConfig.params.pageNo>0?currentContainerConfig.params.pageNo:currentContainerConfig.currPageNo;
		currentContainerConfig.params.pageSize = currentContainerConfig.params.pageSize || currentContainerConfig.defaultPageSize;
		$.post(currentContainerConfig.url, currentContainerConfig.params, function(data) {
			createTableBody(data);
			if(ispage!=false) {
				createPagingBar(data);
			}
		});

	};

})(jQuery);

